import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class SavePanel extends JPanel implements ActionListener {
    private Icon bgOff = Util.getIcon("/images/buttonOff.jpg");
    private Icon bgSave = Util.getIcon("/images/buttonSave.jpg");
    private Icon bgsure = Util.getIcon("/images/buttonSure.jpg");
    private ATM frame;
    private JButton offButton = new JButton(bgOff);
    private JTextField textField = new JTextField();
    private JButton sureButton = new JButton(bgsure);

    public SavePanel(ATM frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);
        offButton.setBounds(300, 600, 100, 50);
        this.add(offButton);
        offButton.addActionListener(this);
        sureButton.setBounds(300, 500, 100, 50);
        this.add(sureButton);
        sureButton.addActionListener(this);
        JLabel label = new JLabel("存款金额");
        label.setBounds(350, 300, 100, 20);
        this.add(label);
        textField.setBounds(400, 300, 100, 20);
        this.add(textField);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == offButton) {
            frame.switchPanel("menu");
        } else if (e.getSource() == sureButton) {
            double yue = frame.getList().get(frame.getLoginPanel().getIndex()).getMoney();
            double shuru = Double.valueOf(textField.getText());
            double cun = yue + shuru;
            frame.getList().get(frame.getLoginPanel().getIndex()).setMoney(cun);
            String sql = "update t_user set u_money=" + frame.getList().get(frame.getLoginPanel().getIndex()).getMoney()+" where u_name='"+frame.getList().get(frame.getLoginPanel().getIndex()).getMoney()+"'";;
            Util.insert(sql);
            frame.switchPanel("menu");
        }
    }
}